<?php
session_start();
include("load-settings.php");

if(!isset($_SESSION['user']))
	header("Location: login.php");

$user = $_SESSION['user'];

$result = mysql_query("SELECT * FROM user WHERE id = $user");
$row = mysql_fetch_array($result);

$type = $row['type'];
$level = $row['level'];
$username = $row['username'];

function numberToRoman($num) 
 {
     // Make sure that we only use the integer portion of the value
     $n = intval($num);
     $result = '';
 
     // Declare a lookup array that we will use to traverse the number:
     $lookup = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400,
     'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40,
     'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1);
 
     foreach ($lookup as $roman => $value) 
     {
         // Determine the number of matches
         $matches = intval($n / $value);
 
         // Store that many characters
         $result .= str_repeat($roman, $matches);
 
         // Substract that from the number
         $n = $n % $value;
     }
 
     // The Roman numeral should be built, return it
     return $result;
 }
 function romanToNumber($roman)
 {
	 $romans = array(
    'M' => 1000,
    'CM' => 900,
    'D' => 500,
    'CD' => 400,
    'C' => 100,
    'XC' => 90,
    'L' => 50,
    'XL' => 40,
    'X' => 10,
    'IX' => 9,
    'V' => 5,
    'IV' => 4,
    'I' => 1,
);

$result = 0;

foreach ($romans as $key => $value) {
    while (strpos($roman, $key) === 0) {
        $result += $value;
        $roman = substr($roman, strlen($key));
    }
}
return $result;
}
	$result = mysql_query("SELECT * FROM duo_order WHERE complete = 0");

	$unclamedArray = array();
	$claimedArray = array();

	$z = 0;
	$y = 0;

	$html_buffer = '';

	while($row = mysql_fetch_array($result))
	{
		
		/*if($row['start_tier'] == "Bronze" || $row['start_tier'] == "Silver" || $row['start_tier'] == "Gold")
			$status_req = 1;
		else if($row['start_tier'] == "Platinum" || $row['start_tier'] == "Diamond")
			$status_req = 2;
		else if($row['start_tier'] == "Challenger")
			$status_req = 3;*/

		
							if($row['start_tier'] == "Bronze" || $row['start_tier'] == "Silver")
							{
								$status_req = 1;
							}
							else if($row['start_tier'] == "Gold")
							{
								if($row['start_division'] == 5)
								{
									$status_req = 1;
								}
								if($row['start_division'] == 4)
								{
									$status_req = 2;
								}
								if($row['start_division'] == 3)
								{
									$status_req = 3;
								}
								if($row['start_division'] == 2)
								{
									$status_req = 4;
								}
								if($row['start_division'] == 1)
								{
									$status_req = 5;
								}
							}
							else if($row['start_tier'] == "Platinum")
							{
								if($row['start_division'] == 5)
								{
									$status_req = 6;
								}
								if($row['start_division'] == 4)
								{
									$status_req = 7;
								}
								if($row['start_division'] == 3)
								{
									$status_req = 8;
								}
								if($row['start_division'] == 2)
								{
									$status_req = 9;
								}
								if($row['start_division'] == 1)
								{
									$status_req = 10;
								}
							}
							else if($row['start_tier'] == "Diamond")
							{
								if($row['start_division'] == 5)
								{
									$status_req = 11;
								}
								if($row['start_division'] == 4)
								{
									$status_req = 12;
								}
								if($row['start_division'] == 3)
								{
									$status_req = 13;
								}
								if($row['start_division'] == 2)
								{
									$status_req = 14;
								}
								if($row['start_division'] == 1)
								{
									$status_req = 15;
								}
							}
							else if($row['start_tier'] == "Challenger")
							{
								$status_req = 16;
							}
							
		
		
			$html_buffer .= '<tr class=""><td class="center ">'.$row['id'].'</td>';
			$html_buffer .= '<td style="text-align: center;" class=" "><small>'.date("d M Y", $row['timestamp']).'</small>';
			
			$subresult = mysql_query("SELECT * FROM user WHERE id = ".$row['user']);
			$subrow = mysql_fetch_array($subresult);

			if($row['region'] == "na")
				$region = "North America";
			else if($row['region'] == "br")
				$region = "Brazil";
			else if($row['region'] == "lan")
				$region = "Latin America North";
			else if($row['region'] == "eun")
				$region = "Europe East";
			else if($row['region'] == "euw")
				$region = "Europe West";
			else if($row['region'] == "ru")
				$region = "Russia";
			else if($row['region'] == "tr")
				$region = "Turkey";

			require_once("unirest/lib/Unirest.php");

			//$region = $row['region'];
			$summoner = str_replace(" ", "", $row['summoner']);

			$tier = "";
			$rank = "";
			$last_match_result = mysql_query("SELECT * FROM match_history WHERE order_id = ".$row['id']." ORDER BY timestamp DESC LIMIT 1") or die(mysql_error());
			//$tier = $row['start_tier'];
			//$rank = $row['start_division'];
			if(mysql_num_rows($last_match_result) == 0)
			{
				$lp = $row['beginning_lp'];
				$tier = $row['start_tier'];
				$rank = numberToRoman($row['start_division']);
			}
			else
			{
				$last_match = mysql_fetch_array($last_match_result);
				$lp = $last_match['lp_at_end'];
				if($last_match['tier_at_end'] != null && $last_match['tier_at_end'] != "")
				{
					$tier = $last_match['tier_at_end'];
				}
				if($last_match['rank_at_end'] != null && $last_match['rank_at_end'] != "")
				{
					$rank = $last_match['rank_at_end'];
				}

				//$tier = $last_match['tier_at_end'];
				//$rank = $last_match['rank_at_end'];
			}
			if($tier == "")
			{

				$lp = $row['beginning_lp'];
				$tier = $row['start_tier'];
				$rank = numberToRoman($row['start_division']);
			}
			$html_buffer .= '</td><td class=" ">'.$region.'<br><span class="label label-success">online</span></td>';
			$html_buffer .= '<td class="center "><img src="/_FILES/images/'.strtolower($tier).'_'.romanToNumber($rank).'.png" height="50px" width="50px" class="tier"><br>';
			$html_buffer .= '<small><b>'.$tier.' '.$rank.'</b></small><br>';
			$html_buffer .= '<small>LP: '.$lp.'</span></small>';
			
			if($row['number_of_games'] > 0)
				$html_buffer .= '<td class="center ">'.$row['number_of_games'].' games</small></td>';
			else if($row['number_of_wins'] > 0)
				$html_buffer .= '<td class="center ">'.$row['number_of_wins'].' wins</small></td>';
			else if($row['number_of_coaches'] > 0)
				$html_buffer .= '<td class="center ">'.$row['number_of_coaches'].' coached games</small></td>';
			$html_buffer .= '<td class="center ">$'.($row['booster_payout']).'</td>';

			$booster_result = mysql_query("SELECT * FROM booster_duo WHERE active <> 0 AND duo_order = ".$row['id']);
			if(mysql_num_rows($booster_result) != 0)
			{
				$booster_row = mysql_fetch_array($booster_result);

				$net_wins = $booster_row['wins'] - $booster_row['losses'];

				if($row['number_of_games'] > 0)
					$percent = round(($net_wins / $row['number_of_games']) * 100);
				else if($row['number_of_wins'] > 0)
					$percent = round(($net_wins / $row['number_of_wins']) * 100);
				else if($row['number_of_coaches'] > 0)
					$percent = round(($net_wins / $row['number_of_coaches']) * 100);

				$label = "label-blue";
				$user_result = mysql_query("SELECT * FROM user WHERE id = ".$booster_row['booster']);
				$user_row = mysql_fetch_array($user_result);
				$booster = $user_row['username'];
			}
			else
			{
				$percent = 0;
				$label = "label-green";
				$booster = "No one";
			}
			$html_buffer .= '<td class="center "><span class="label '.$label.'">'.$booster.'</span></td>';

			if($booster == $username)
			{
				$html_buffer .= '<td class="center"><a href="duo_order.php?id='.$row['id'].'" class="btn btn-success btn-xs">Details</a><a href="unclaim_duo.php?id='.$row['id'].'" class="btn btn-success btn-xs" onclick = "check_confirm(event)">Unclaim</a></td></tr>';

				$claimedArray[$y] = $html_buffer;
				$y++;
			}
			else if($booster != "No one")
			{
				if($type == 2 || $type == 3)
					$html_buffer .= '<td class="center"><a href="duo_order.php?id='.$row['id'].'" class="btn btn-success btn-xs">Details</a></td></tr>';
				else
					$html_buffer .= '<td class="center"></td></tr>';

				$claimedArray[$y] = $html_buffer;
				$y++;
			}
			else
			{
				if($level >= $status_req || $type == 2 || $type == 3)
				{
					$html_buffer .= '<td class="center"><a href="claim_duo.php?id='.$row['id'].'" class="btn btn-success btn-xs" onclick = "check_confirm(event)">Claim</a><a href="duo_order.php?id='.$row['id'].'" class="btn btn-success btn-xs">Details</a></td></tr>';
				}

				$unclaimedArray[$z] = $html_buffer;
				$z++;
			}

			
		
		$html_buffer = '';
	}

	$x = 0;

	while(isset($unclaimedArray[$x]))
	{
		echo $unclaimedArray[$x];
		$x++;
	}

	$x = 0;

	while(isset($claimedArray[$x]))
	{
		echo $claimedArray[$x];
		$x++;
	}
?>